package com.vf.admin.common.splitpage;


/**
 * 分页对象；从前台传递到后台；
 */
public class SplitPageBean {

    private int pageIndex = 1; 		// 当前页
    private int pageSize = 10; 		// 每页记录个数
    private int totalCount = 0; 	// 总记录数

    public SplitPageBean(){}
    /**
     * 取分页记录
     *
     * @param page 当前页面序号(以1开始)
     * @param limit 每页记录数
     */
    public SplitPageBean(Integer page, Integer limit) {
        if (page==null || page < 1) {
            this.pageIndex = 1;
        } else {
            this.pageIndex = page;
        }

        if (limit==null || limit < 1) {
            this.pageSize = 20;
        } else {
            this.pageSize = limit;
        }
    }

    public SplitPageBean(int page, int limit, int count) {
        this(page, limit);
        this.totalCount = totalCount;
    }

    /**
     * 初始化bean
     * @param page 当前页索引
     */
    public SplitPageBean(Integer page) {
        if (page < 1) {
            this.pageIndex = 1;
        } else {
            this.pageIndex = page;
        }
    }

    /**
     * 获取起始记录索引；后台使用(first record)
     *
     * @return
     */
    public int getFirstRecord() {
        int firstRecord = (pageIndex - 1) * pageSize;
//        System.out.println("firstRecord:"+firstRecord+"  totalCount:"+totalCount);
//        if(firstRecord > totalCount){
//            return 0;
//        }else{
        return (pageIndex - 1) * pageSize;
//        }

    }

    /**
     * 获取记录数量；后台使用 (max record)
     *
     * @return
     */
    public int getMaxRecord() {
//		if(totalCount == 0){
//			throw new RuntimeException("请在获取最大记录数前，首先为 totalCount属性赋值.");
//		}
//        System.out.println("getFirstRecord:"+getFirstRecord()+"  totalCount:"+totalCount);
        if (getFirstRecord() > totalCount) {
            // 由程序处理
            return 0;
        } else {
            if ((getFirstRecord() + pageSize) > totalCount) {
//                System.out.println(getFirstRecord()+"---"+pageSize+"----"+totalCount);
                return totalCount - getFirstRecord() + 1;
            } else {
                return pageSize;
            }
        }
    }

    public int getPageIndex() {
        return pageIndex;
    }


    /**
     * 设置每页记录数
     * @param limit
     */
    public void setPageSize(int limit){
        this.pageSize = limit;
    }

    public int getPageSize(){
        return this.pageSize;
    }

    /**
     * 得到总记录数
     */
    public int getTotalCount() {
        return totalCount;
    }

    /**
     * 设置总记录数
     * @param totalRecordCount
     */
    public void setTotalCount(int totalRecordCount) {
        this.totalCount = totalRecordCount;
    }

    /**
     * 得到总页面数
     *
     * @return
     */
    public int getTotalPageCount() {
        int totalPageCount = totalCount / pageSize;
        if (totalCount % pageSize == 0) {
            return totalPageCount;
        } else {
            return totalPageCount + 1;
        }
    }

    /**
     * 设置当前页索引
     * @param page
     */
    public void setPageIndex(int page) {
        this.pageIndex = page;
    }

    /**
     * 当前页码的下一页页码
     */
    public int getNextPageNo(){
        int sumPageSize = this.getPageSize()*this.getPageIndex();
        if(this.getTotalCount()>sumPageSize){
            return this.getPageIndex()+1;
        }else{
            return -1;  //下一页不存在
        }
    }
}
